package com.jade.zsy.sales.facade.impl;

import com.jade.bss.DbException;
import com.jade.bss.base.customer.Customer;
import com.jade.bss.base.utils.CopyBean;
import com.jade.bss.db.impl.BaseFacadeImpl;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.zsy.base.common.StatusConstant;
import com.jade.zsy.base.entity.BusinessCode;
import com.jade.zsy.base.facade.BusinessCodeFacade;
import com.jade.zsy.inventory.entity.Inventory;
import com.jade.zsy.inventory.service.InventoryFacade;
import com.jade.zsy.finance.entity.Receivable;
import com.jade.zsy.finance.service.ReceivableFacade;
import com.jade.zsy.sal.enity.SO;
import com.jade.zsy.sal.enity.Shipment;
import com.jade.zsy.sal.enity.ShipmentDetail;
import com.jade.zsy.sal.service.SOFacade;
import com.jade.zsy.sal.service.ShipmentDetailFacade;
import com.jade.zsy.sal.service.ShipmentFacade;
import com.jade.zsy.sal.vo.SOVo;
import com.jade.zsy.sal.vo.ShipmentVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by Adminstrator on 2017/6/20.
 */
@Service("ShipmentFacade")
public class ShipmentFacadeImpl extends BaseFacadeImpl implements ShipmentFacade {


    @Override
    @Transactional
<<<<<<< HEAD
    public void save(Object obj)throws DbException{
        SOVo soVo = (SOVo)obj;
        SO so  = soFacade.getById(SO.class,soVo.getId());
        Shipment shipment = null;
        try {
            shipment = CopyBean.copy(so,Shipment.class);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        shipment.setRecordNo(businessCodeFacade.nextBusinessCode(BusinessCode.KEY_SALES_JH_SO));
        List<ShipmentDetail> shipmentDetails = null;
        try {
            shipmentDetails = CopyBean.copy(soVo.getSoDetailList(),ShipmentDetail.class);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        super.save(shipment);
        super.batchSave(shipmentDetails);
        int size = shipmentDetails.size();
        float money=0f;
        for(int i=0;i<size;i++){
            ShipmentDetail shipmentDetail =shipmentDetails.get(i);
            Inventory inventory = inventoryFacade.getInventoryBySkuId(shipmentDetail.getSkuId());
            int qty = inventory.getQty();
            int num = shipmentDetail.getNumber();
            if(qty<num){
                throw new DbException("库存不足，不能生成销售单","inventory is not enough");
            }else {
                inventory.setQty(qty-num);
            }
            inventoryFacade.update(inventory);
            money += shipmentDetail.getNumber()*shipmentDetail.getUnitPrice().floatValue();
        }
        Receivable receivable = new Receivable();
        receivable.setEmployeeId(shipment.getCreatorId());
        receivable.setOrgId(shipment.getOrgId());
        receivable.setRecordDate(new Date().toString());
        receivable.setCreatorId(shipment.getCreatorId());
        receivable.setCreator(shipment.getCreator());
        receivable.setMoney(new BigDecimal(money));
        receivable.setRecordNo(businessCodeFacade.nextBusinessCode(BusinessCode.KEY_SALES_RECEIVABLE));
        receivable.setType("sales");
        receivable.setStatus(StatusConstant.RECORD_SUBMIT);
        receivableFacade.save(receivable);
=======
    public void save(SOVo soVo)throws Exception{

>>>>>>> jinya
    }

    @Transactional
    public void delete(Object obj)throws Exception{
        Shipment shipment = (Shipment)obj;
        super.delete(Shipment.class,shipment.getId());
    }

    @Transactional
    public void update(ShipmentVo shipmentVo)throws Exception{
        Shipment shipment = CopyBean.copy(shipmentVo,Shipment.class);
        super.update(shipment);
    }

    public Shipment query(String id)throws Exception{
        Shipment shipment = this.getById(Shipment.class,id);

        return shipment;
    }

    @Transactional
    public ShipmentVo addRefund()throws Exception{
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        Shipment shipment = new Shipment();

        return CopyBean.copy(shipment,ShipmentVo.class);
    }

    @Transactional
    public void saveRefund(ShipmentVo shipmentVo)throws Exception{
        Shipment shipment = CopyBean.copy(shipmentVo,Shipment.class);

    }

}
